/**
 * project: finance-manage
 * package: com.example.dataAccess
 * 数据库 > 操作数据表
 */

package com.example.dataAccess;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.example.interfaces.DataDAOInterface;

public class DataDAO extends DAO_Base implements DataDAOInterface {
    
    public DataDAO(Connection connection) {
        super(connection);
    }

    public DataDAO() throws IOException, SQLException {
        super();
    }

    /* 插入数据 */
    @Override
    public boolean insert(String tableName, String type, double amount, String date, String statement, String notes) throws SQLException {
        String sql = "INSERT INTO " + tableName + "(type, amount, date, statement, notes) VALUES (?,?,?,?,?);";
        return executeUpdate(sql, type, amount, date, statement, notes);
    }

    /* 删除数据 */
    @Override
    public boolean delete(String tableName, int bid) throws SQLException {
        String sql = "DELETE FROM " + tableName + " WHERE bid=?";
        return executeUpdate(sql, bid);
    }

    /* 更新数据 */
    @Override
    public boolean update(String tableName, int bid, String key, Object value) throws SQLException {
        String sql = "UPDATE " + tableName + " SET " + key + "=? WHERE bid=?";
        return executeUpdate(sql, bid, value);
    }

    /* 查询某表格全部数据 */
    @Override
    public ResultSet select(String tableName) throws SQLException {
        String sql = "SELECT * FROM " + tableName;
        return executeQuery(sql);
    }

    /* 查询数据 查询条数*/
    @Override
    public ResultSet select(String tableName, int count) throws SQLException {
        String sql = "SELECT * FROM " + tableName + " LIMIT " + count;
        return executeQuery(sql);
    }

    /* 查询数据 根据条件查询 */
    @Override
    public ResultSet select(String tableName, String key, Object value) throws SQLException {
        String sql = "SELECT * FROM " + tableName + " WHERE " + key + "=?";
        return executeQuery(sql, value);
    }
}
